package com.teach.javafxclient.controller.base;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import kong.unirest.json.JSONArray;

import java.util.*;


public class CourseTableController {
    @FXML
    private GridPane gridPane;
    @FXML
    private StackPane stackPane1;
    @FXML
    private StackPane stackPane2;
    @FXML
    private StackPane stackPane3;
    @FXML
    private StackPane stackPane4;
    @FXML
    private StackPane stackPane5;
    @FXML
    private StackPane stackPane6;
    @FXML
    private StackPane stackPane7;
    @FXML
    private StackPane stackPane8;
    @FXML
    private StackPane stackPane9;
    @FXML
    private StackPane stackPane10;
    @FXML
    private StackPane stackPane11;
    @FXML
    private StackPane stackPane12;
    @FXML
    private StackPane stackPane13;
    @FXML
    private StackPane stackPane14;
    @FXML
    private StackPane stackPane15;
    @FXML
    private StackPane stackPane16;
    @FXML
    private StackPane stackPane17;
    @FXML
    private StackPane stackPane18;
    @FXML
    private StackPane stackPane19;
    @FXML
    private StackPane stackPane20;
    @FXML
    private StackPane stackPane21;
    @FXML
    private StackPane stackPane22;
    @FXML
    private StackPane stackPane23;
    @FXML
    private StackPane stackPane24;
    @FXML
    private StackPane stackPane25;
    @FXML
    private StackPane stackPane26;
    @FXML
    private StackPane stackPane27;
    @FXML
    private StackPane stackPane28;
    @FXML
    private StackPane stackPane29;
    @FXML
    private StackPane stackPane30;
    @FXML
    private StackPane stackPane31;
    @FXML
    private StackPane stackPane32;
    @FXML
    private StackPane stackPane33;
    @FXML
    private StackPane stackPane34;
    @FXML
    private StackPane stackPane35;
    JSONArray jsonArray = new JSONArray();
    // 用于存储StackPane与time的关联
    final private Map<StackPane, String> stackPaneToTimeMap = new HashMap<>();
    private Map<Integer,String> weekDayOrder = new HashMap<>();

    public void initialize(){
        setDayToDay(weekDayOrder);
        // ... 为其他StackPane添加点击监听器 ...
        addClickListener(stackPane1);addClickListener(stackPane2);addClickListener(stackPane3);addClickListener(stackPane4);addClickListener(stackPane5);addClickListener(stackPane6);addClickListener(stackPane7);addClickListener(stackPane8);
        addClickListener(stackPane9);addClickListener(stackPane10);addClickListener(stackPane11);addClickListener(stackPane12);addClickListener(stackPane13);addClickListener(stackPane14);addClickListener(stackPane15);addClickListener(stackPane16);
        addClickListener(stackPane17);addClickListener(stackPane18);addClickListener(stackPane19);addClickListener(stackPane20);addClickListener(stackPane21);addClickListener(stackPane22);addClickListener(stackPane23);addClickListener(stackPane24);
        addClickListener(stackPane25);addClickListener(stackPane26);addClickListener(stackPane27);addClickListener(stackPane28);addClickListener(stackPane29);addClickListener(stackPane30);addClickListener(stackPane31);addClickListener(stackPane32);
        addClickListener(stackPane33);addClickListener(stackPane34);addClickListener(stackPane35);
        //printAllTimes();
    }

    //设置事件处理器
    private void addClickListener(StackPane stackPane) {
        stackPane.setOnMouseClicked(event -> {
            //更新状态
            toggleSelection(stackPane);
            //printAllTimes();
        });
}
    //更新方法
    private void toggleSelection(StackPane stackPane) {
        // 切换选中状态，使用背景颜色变化示例，同时更新jsonArray
        if (stackPane.getBackground() == null ||
                stackPane.getBackground().getFills().isEmpty() ||
                stackPane.getBackground().getFills().get(0).getFill().equals(Color.TRANSPARENT)) {
            stackPane.setBackground(new Background(new BackgroundFill(Color.web("#F8F5D6"), null, null)));
            updateAddJSONArray(stackPane);
        } else {
            stackPane.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, null, null)));
            updateDeleteJSONArray(stackPane);
        }
    }
    //更新JSONArray的方法
    public void updateAddJSONArray(StackPane stackPane){
        // 获取StackPane在GridPane中的行和列索引
        int lessonNumber = GridPane.getRowIndex(stackPane);
        int dayOfWeek = GridPane.getColumnIndex(stackPane);

        // 验证索引是否有效
        if (lessonNumber < 1 || lessonNumber > 5 || dayOfWeek < 1 || dayOfWeek > 7) {
            throw new IllegalArgumentException("StackPane is not positioned properly in the GridPane");
        }

        // 创建JSONArray并添加星期和课程时间
        JSONArray innerArray = new JSONArray();
        innerArray.put(dayOfWeek); // 星期几
        innerArray.put(lessonNumber); // 第几节课
        setInner(stackPane,innerArray);
        jsonArray.put(innerArray);
    }//FIXME:JSONArray排序方法
    public void updateDeleteJSONArray(StackPane stackPane){
        // 获取StackPane在GridPane中的行和列索引
        int lessonNumber = GridPane.getRowIndex(stackPane);
        int dayOfWeek = GridPane.getColumnIndex(stackPane);

        // 验证索引是否有效
        if (lessonNumber < 1 || lessonNumber > 5 || dayOfWeek < 1 || dayOfWeek > 7) {
            throw new IllegalArgumentException("StackPane is not positioned properly in the GridPane");
        }

        for(int i=0;i<jsonArray.length();i++){
            JSONArray innerArray = jsonArray.getJSONArray(i);
            int columnIndex = innerArray.getInt(0); // 获取星期几
            int rowIndex = innerArray.getInt(1); // 获取第几节课
            if ((columnIndex == dayOfWeek) && (rowIndex == lessonNumber)) {
                set_Delete(stackPane,innerArray);
                jsonArray.remove(i);
            }
        }
    }

    //set方法
    public void setInner(StackPane stackPane, JSONArray innerArray) {
        if (!innerArray.isEmpty()) {
            int dayOfWeek = innerArray.getInt(0); // 获取星期几
            int lessonNumber = innerArray.getInt(1); // 获取第几节课

            // 构造time字符串，例如 "周一第3节"
            String time = weekDayOrder.get(dayOfWeek) + "第" + lessonNumber + "节";

            // 设置StackPane的time属性
            stackPaneToTimeMap.put(stackPane, time);

        }
    }
    
    public void set_Delete(StackPane stackPane, JSONArray innerArray) {
        if (!innerArray.isEmpty()) {
            stackPaneToTimeMap.remove(stackPane);
        }
    }

    //外部类调用的set方法，传入的二维JSONArray数组
    public void set(JSONArray jsonArray) {
        if (!jsonArray.isEmpty()) {
            for(int i=0;i<jsonArray.length();i++){
                JSONArray innerArray = jsonArray.getJSONArray(i);
                int dayOfWeek = innerArray.getInt(0);// 获取星期几
                int lessonNumber = innerArray.getInt(1);// 获取第几节课
                Optional<StackPane> optionalStackPane = getStackPaneAt(gridPane, lessonNumber, dayOfWeek);
                // 对找到的StackPane进行操作
                optionalStackPane.ifPresent(this::toggleSelection);
            }
        }
    }

    /**
     * 尝试从GridPane中获取指定行和列索引的StackPane
     *
     * @param gridPane GridPane实例
     * @param rowIndex 行索引
     * @param colIndex 列索引
     * @return 包含StackPane的Optional，如果未找到则为空
     */
    private Optional<StackPane> getStackPaneAt(GridPane gridPane, int rowIndex, int colIndex) {
        for (Node node : gridPane.getChildren()) {
            if (node instanceof StackPane) {
                if (GridPane.getRowIndex(node) == rowIndex && GridPane.getColumnIndex(node) == colIndex) {
                    return Optional.of((StackPane) node);
                }
            }
        }
        return Optional.empty();
    }


    //Print方法
//    private void printAllTimes() {
//        for (String time : stackPaneToTimeMap.values()) {
//            System.out.print(time+"   ");
//        }
//        System.out.println();
//    }

    public void setDayToDay(Map<Integer, String> weekDayOrder) {
        this.weekDayOrder = weekDayOrder;
        weekDayOrder.put(1, "周一");
        weekDayOrder.put(2, "周二");
        weekDayOrder.put(3, "周三");
        weekDayOrder.put(4, "周四");
        weekDayOrder.put(5, "周五");
        weekDayOrder.put(6, "周六");
        weekDayOrder.put(7, "周日");
    }

}
